package cn.caplike.demo.netty.http.quickstart;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author LiKe
 * @version 1.0.0
 * @date 2021-11-26 17:34
 */
public class TestServer {

    public static void main(String[] args) throws InterruptedException {
        // ~ 定义两个线程组, 其实就是两个死循环
        EventLoopGroup parentGroup = new NioEventLoopGroup(); // 不断的接收连接, 转给 childGroup
        EventLoopGroup childGroup = new NioEventLoopGroup(); // 真正完成对连接的处理

        try {
            // 启动服务端
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap
                // Set the EventLoopGroup for the parent (acceptor) and the child (client).
                // These EventLoopGroup's are used to handle all the events and IO for ServerChannel and Channel's.
                .group(parentGroup, childGroup)
                .channel(NioServerSocketChannel.class)
                // ~ 子处理器, 真正的处理请求
                .childHandler(new TestServerChannelInitializer());

            final ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); // curl 127.0.0.1:8899
            channelFuture.channel().closeFuture().sync();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }

}
